﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPPlanManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Windows;
using Tecnomatix.Engineering.Olp;


namespace DnProcessSimulateCommands;

public class CApAPPPlanManager
{
  private CApAPPPlanCommandBase m_planCommand;
  private List<CApAPPPlanCommandBase> m_planCommands;
  internal const int NumberOfSubTaskWithoutScroolbar = 3;
  private double m_currentTime;
  private int m_previousProgressIndex;
  private double m_previousProgressTime;
  private List<List<bool>> m_planningList;
  private KiAppRobotSimulationQuery m_simQuery;
  private TxPathPlanningSimulationHandler m_simHandler;
  private int m_currentIndex;
  private bool m_atLeastOneFailingCommand;
  private bool m_errorMessageAlreadyDisplayed;
  private string m_lastErrorMessage = "";
  private string m_lastErrorDetails = "";

  public event PlanningWillEndEventHandler PlanningWillEnd;

  public event PlanningDidEndEventHandler PlanningDidEnd;

  public event PlanningDidProgressEventHandler PlanningDidProgress;

  public event PlanningDidUpdateEventHandler PlanningDidUpdate;

  public event WaypointTypeDidChangedEventHandler WaypointDidChanged;

  public CApAPPPlanManager(
    List<CApAPPTask> i_tasks,
    KiAppPathPlanningParamsEx i_pathPlanningParams,
    List<CApAPPPlanningType> i_plannings,
    bool i_preProcessingActivated)
  {
    this.m_currentTime = 0.0;
    this.m_previousProgressTime = 0.0;
    this.m_currentIndex = 0;
    this.m_previousProgressIndex = 0;
    this.m_planningList = new List<List<bool>>();
    this.m_planCommands = new List<CApAPPPlanCommandBase>();
    foreach (CApAPPPlanningType iPlanning in i_plannings)
    {
      CApAPPPlanCommandBase appPlanCommandBase = (CApAPPPlanCommandBase) null;
      switch (iPlanning)
      {
        case CApAPPPlanningType.PATHPLANNING:
        case CApAPPPlanningType.PATHPLANNING_AND_CLEARANCE_OPT:
        case CApAPPPlanningType.PATHPLANNING_AND_JOINTTRAVEL_OPT:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPPlanCommand(i_tasks, i_pathPlanningParams, iPlanning != 0, i_preProcessingActivated);
          break;
        case CApAPPPlanningType.PATHPLANNING_AND_CYCLETIME_OPT:
        case CApAPPPlanningType.PATHPLANNING_AND_ENERGY_OPT:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPPlanCommand(i_tasks, i_pathPlanningParams, iPlanning != 0, i_preProcessingActivated);
          this.CreateRobotSimulationData();
          break;
        case CApAPPPlanningType.CLEARANCE_OPT:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPOptimizeClearanceCommand(i_tasks, i_pathPlanningParams);
          break;
        case CApAPPPlanningType.CYCLETIME_OPT:
        case CApAPPPlanningType.ENERGY_OPT:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPOptimizeCycleTimeCommand(i_tasks, i_pathPlanningParams);
          this.CreateRobotSimulationData();
          break;
        case CApAPPPlanningType.JOINTTRAVEL_OPT:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPOptimizeLengthCommand(i_tasks, i_pathPlanningParams);
          break;
        case CApAPPPlanningType.APPROACH_DEPART_CREATION:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPApproachDepartPlanCommand(i_tasks, i_pathPlanningParams, i_pathPlanningParams.OpeningGunValue, i_pathPlanningParams.PerpendicularAdjustement);
          break;
        case CApAPPPlanningType.PATHPLANNING_POST_PROCESS:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPPostProcessingCommand(i_tasks, i_pathPlanningParams);
          this.CreateRobotSimulationData();
          break;
        case CApAPPPlanningType.CUSTOM_PATH_SEGMENTS_PLANNING:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPKineoForAppPathSegmentPlanCommand(i_tasks, i_pathPlanningParams);
          break;
        case CApAPPPlanningType.CUSTOM_TRAJECTORY_SEGMENTS_PLANNING:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPKineoForAppTrajectorySegmentPlanCommand(i_tasks, i_pathPlanningParams);
          this.CreateRobotSimulationData();
          break;
        case CApAPPPlanningType.CUSTOM_WHOLE_TRAJECTORY_PLANNING:
          appPlanCommandBase = (CApAPPPlanCommandBase) new CApAPPKineoForAppWholeTrajectoryPlanCommand(i_tasks, i_pathPlanningParams);
          this.CreateRobotSimulationData();
          break;
      }
      appPlanCommandBase.PlanningDidProgress += new PlanningDidProgressEventHandler(this.m_planCommand_PlanningDidProgress);
      appPlanCommandBase.PlanningDidEnd += new PlanningDidEndEventHandler(this.m_planCommand_PlanningDidEnd);
      appPlanCommandBase.PlanningDidUpdate += new PlanningDidUpdateEventHandler(this.m_planCommand_PlanningDidUpdate);
      appPlanCommandBase.WaypointDidChanged += new WaypointTypeDidChangedEventHandler(this.m_planCommand_WaypointStatusDidChange);
      int capacity = appPlanCommandBase.CountSubProblems();
      List<bool> boolList = new List<bool>(capacity);
      for (int index = 0; index < capacity; ++index)
        boolList.Add(false);
      this.m_planningList.Add(boolList);
      this.m_planCommands.Add(appPlanCommandBase);
    }
    this.m_planCommand = this.m_planCommands[this.m_currentIndex];
  }

  public void Start()
  {
    CApAPPPlanCommandBase.PlanSuccessEventArgs errEvtArgs = (CApAPPPlanCommandBase.PlanSuccessEventArgs) null;
    this.m_planCommand.Start(ref errEvtArgs);
  }

  public CApAPPPlanCommandBase PlanCommand => this.m_planCommand;

  public void SkipPlanning(int i_index) => this.m_planCommand.CancelPlanning(i_index);

  public void SkipPlanning(CApAPPWaypoint i_waypoint)
  {
    int i_taskRank = this.m_planCommand.RetrieveIndexSubProblemFromWaypoint(i_waypoint);
    if (i_taskRank == -1 || i_taskRank >= this.m_planCommand.CountSubProblems())
      return;
    this.m_planCommand.CancelPlanning(i_taskRank);
  }

  public void CancelPlanning(int index) => this.m_planCommand.CancelPlanning(index);

  public void StopPlanning() => this.m_planCommand.Cancel();

  public void ClearSimQueries()
  {
    if (this.m_simQuery == null)
      return;
    this.m_simQuery.ClearSimFunc();
    this.m_simQuery = (KiAppRobotSimulationQuery) null;
  }

  private void m_planCommand_WaypointStatusDidChange(object sender, EventArgs args)
  {
    if (this.WaypointDidChanged == null)
      return;
    this.WaypointDidChanged((object) this, args);
  }

  private void m_planCommand_PlanningDidUpdate(object sender, EventArgs args)
  {
    if (this.PlanningDidUpdate == null)
      return;
    this.PlanningDidUpdate((object) this, args);
  }

  private void m_planCommand_PlanningDidProgress(
    object sender,
    CApAPPPlanCommandBase.PlanDidProgressEventArgs args)
  {
    double num = 0.0;
    if (this.m_currentIndex < this.m_planningList.Count)
    {
      for (int index = 0; index < this.m_planningList[this.m_currentIndex].Count; ++index)
      {
        if (!this.m_planningList[this.m_currentIndex][index] && this.m_planCommand.IsFinished(index))
        {
          this.m_planningList[this.m_currentIndex][index] = true;
          this.m_planCommand.UpdatePlanSuccesses();
        }
      }
    }
    if (this.PlanningDidProgress != null)
    {
      if (this.m_previousProgressIndex != this.m_currentIndex)
      {
        this.m_previousProgressIndex = this.m_currentIndex;
        this.m_previousProgressTime = this.m_currentTime + this.m_previousProgressTime;
      }
      args.TimeInSecond += this.m_previousProgressTime;
      args.Progress = args.Progress / (double) this.m_planningList.Count + (double) this.m_currentIndex * (1.0 / (double) this.m_planningList.Count);
      this.PlanningDidProgress((object) this, args);
      this.m_currentTime = args.TimeInSecond;
    }
    if (num < 1.0 || this.PlanningWillEnd == null)
      return;
    this.PlanningWillEnd((object) this, new EventArgs());
  }

  private void m_planCommand_PlanningDidEnd(
    object sender,
    CApAPPPlanCommandBase.PlanSuccessEventArgs e)
  {
    if (this.IsMagnetPointEnabled())
      this.ToggleMagnetPoint();
    this.ClearMagnetPoint();
    ++this.m_currentIndex;
    if (this.m_currentIndex < this.m_planCommands.Count)
    {
      if (!e.Successful)
      {
        this.m_atLeastOneFailingCommand = true;
        this.m_lastErrorMessage = e.ErrorMessage;
        this.m_lastErrorDetails = e.ErrorDetails;
      }
      if (this.PlanningDidUpdate != null)
        this.PlanningDidUpdate((object) this, new EventArgs());
      this.m_planCommand = this.m_planCommands[this.m_currentIndex];
      this.m_planCommand.PlanningWasCancelled = this.m_planCommands[this.m_currentIndex - 1].PlanningWasCancelled;
      this.m_planCommand.UpdateSubTaskToPlan();
      this.Start();
    }
    else
    {
      if (!e.Successful && this.m_errorMessageAlreadyDisplayed)
        e.Successful = true;
      if (e.Successful && this.m_atLeastOneFailingCommand)
      {
        e = new CApAPPPlanCommandBase.PlanSuccessEventArgs(false, this.m_lastErrorMessage, this.m_lastErrorDetails);
        this.m_atLeastOneFailingCommand = false;
        this.m_errorMessageAlreadyDisplayed = true;
      }
      if (this.PlanningDidEnd != null)
        this.PlanningDidEnd((object) this, e);
      this.ClearSimQueries();
    }
  }

  internal bool IsMagnetPointEnabled() => this.m_planCommand.IsMagnetPointEnabled();

  internal void EnableShowLastAddedPosition() => this.m_planCommand.EnableShowLastAddedPosition();

  internal void DisableShowLastAddedPosition() => this.m_planCommand.DisableShowLastAddedPosition();

  internal void ToggleMagnetPoint() => this.m_planCommand.ToggleMagnetPoint();

  internal void ClearMagnetPoint() => this.m_planCommand.ClearMagnetPoint();

  internal void SetMagnetPointPositionToLastAddedNode()
  {
    this.m_planCommand.SetMagnetPointPositionToLastAddedNode();
  }

  public KiAppPlanningStep GetCurrentPlanningStep() => this.m_planCommand.GetCurrentPlanningStep();

  private void CreateRobotSimulationData()
  {
    this.m_simQuery = new KiAppRobotSimulationQuery();
    this.m_simHandler = new TxPathPlanningSimulationHandler();
  }

  private delegate void UpdateProgressBarDelegate(DependencyProperty dp, object value);
}
